package cn.jbolt.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.jbolt.common.base.BaseController;
import cn.jbolt.common.exception.AppException;
import cn.jbolt.dao.model.Card;
import cn.jbolt.dao.model.Favcard;
import cn.jbolt.dao.model.SysUser;
@Before(Tx.class)
public class CardController extends BaseController {
	public void getCardList() {
		String searchText = getPara("searchtext");
		if (StrKit.isBlank(searchText)) {
			searchText = "%";
		} else {
			searchText ="%" +searchText + "%";
		}
		int pagenum = getParaToInt("pagenum");
		StringBuffer selectsbf = new StringBuffer();
		StringBuffer sqlExceptSelect = new StringBuffer();
		selectsbf.append("  SELECT              ");
		selectsbf.append("      a.*,b.userid as fav  ");
		sqlExceptSelect.append("  FROM                ");
		sqlExceptSelect.append("      card a          ");
		sqlExceptSelect.append("  left join  favcard b    ");
		sqlExceptSelect.append("  on a.userid=b.userid    ");
		sqlExceptSelect.append("  and a.cardid=b.cardid    ");
		sqlExceptSelect.append("  WHERE               ");
		sqlExceptSelect.append("      a.name LIKE ?   ");
		sqlExceptSelect.append("  OR  a.nj LIKE ?     ");
		sqlExceptSelect.append("  OR  a.zy LIKE ?     ");
		sqlExceptSelect.append("  OR  a.email LIKE ?  ");
		sqlExceptSelect.append("  OR  a.phone LIKE ?  ");
		sqlExceptSelect.append("  OR  a.no LIKE ?     ");
		sqlExceptSelect.append("  OR  a.des LIKE ?    ");
		List<Card> cardList = Card.dao.paginate(pagenum, 5, selectsbf.toString(), sqlExceptSelect.toString(), searchText, searchText, searchText, searchText, searchText,
				searchText, searchText).getList();
		renderJsonResult(cardList);
	}

	public void getMyfavCardList() {
		String userId = getPara("userid");
		StringBuffer sbf = new StringBuffer();
		sbf.append("  SELECT                    ");
		sbf.append("      b.*                   ");
		sbf.append("  FROM                      ");
		sbf.append("      favcard a,      ");
		sbf.append("      card b          ");
		sbf.append("  WHERE                     ");
		sbf.append("      a.userid = ?          ");
		sbf.append("  AND a.cardid = b.cardid   ");
		List<Card> myFavCardList = Card.dao.find(sbf.toString(), userId);
		renderJsonResult(myFavCardList);
	}

	public void getMyCard() {
		String userId = getPara("userid");
		Card myCard = Card.dao.findFirst("select * from card a where a.userid = ?", userId);
		renderJsonResult(myCard);

	}

	public void saveOrUpdateMycard() {
		String jsonStr = HttpKit.readData(getRequest());
		Card card = JsonKit.parse(jsonStr, Card.class);
		String userid = getPara("userid");
		SysUser user = SysUser.dao.findById(userid);
		if (StrKit.isBlank(card.getCardid())) {
			card.setUserid(userid);
			card.setNo(user.getUsername());
			card.setCardid(StrKit.getRandomUUID());
			card.save();
		} else {
			card.update();
		}
		renderJsonSuccess();
	}

	private String genNO() {
		String no = "";
		Card card = Card.dao.findFirst("select max(no) from card ");
		if (StrKit.isBlank(card.getNo())) {
			no = "10000";
		} else {
			no = String.valueOf(Integer.valueOf(card.getNo()) + 1);
		}

		return no;
	}
	public void favOrUnfav(){
		String userid = getPara("userid");
		String cardid = getPara("cardid");
		Card card = Card.dao.findFirst("select * from favcard where userid = ? and cardid = ? ",userid,cardid);
		if(card == null){
			Favcard favcard = new Favcard();
			favcard.setUserid(userid);
			favcard.setCardid(cardid);
			favcard.save();
		}else{
			Favcard.dao.deleteById(cardid,userid);
		}
		renderJsonSuccess();
		
	}
	public void changePassword(){
		String userid = getPara("userid");
		String oldpassword = getPara("oldpassword");
		String newpassword = getPara("password");
		SysUser sysuser = SysUser.dao.findById(userid);
		if(!sysuser.getPassword().equals(oldpassword)){
			throw new AppException("旧密码错误！");
		}
		sysuser.setPassword(newpassword);
		sysuser.update();
		renderJsonSuccess();
	}
}
